home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / strx221.zip / GREP.CPP < prev    next >
C/C++ Source or Header  |  1993-03-17  |  3KB  |  93 lines

  1. //
  2. // grep.cpp: Demo program for str class
  3. // Author  : Roy S. Woll
  4. //
  5. // Copyright (c) 1993 by Roy S. Woll
  6. // You may distribute this source freely as long as you leave all files
  7. // in their original form, including the copyright notice as is.
  8. //
  9. // Version 2.11     03/08/93     Support path specification
  10. // Version 2.00     11/30/92
  11. //
  12. #include <fstream.h>
  13. #include <iomanip.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <dir.h>
  17. #include "str.h"
  18. #include "regx.h"
  19.  
  20. int fileNamesOnly=0;       // true if only file names should be shown
  21. int enableLineNumbers=0;   // true if line numbers should be shown
  22. str line;                  // str used to compare - (case sensitive?)
  23. regX pattern;              // regular expression search pattern
  24.  
  25. void grepfile(const char * curfile)
  26. {
  27.    ifstream searchFile( curfile );
  28.    int foundMatch = 0, lineCount=0, pos;
  29.    str match;
  30.    while (!searchFile.eof()) {
  31.       searchFile >> line;
  32.       lineCount++;
  33.       pos = 0;
  34.       if (line.search(pattern, &match, &pos)){
  35.           if (!foundMatch++){
  36.              if (fileNamesOnly) {
  37.                 cout << "File: " << curfile << endl;
  38.                 return;
  39.              };
  40.              str Bar;
  41.              Bar.stream() << pad("", strlen(curfile)+14, str::right, '*')
  42.                           << endl;
  43.              cout << endl << Bar << "*** File: " << curfile << " ***"
  44.                   << endl << Bar;
  45.           };
  46.           if (enableLineNumbers) cout << setw(4) << lineCount << ": ";
  47.           cout << line << endl;
  48.       };
  49.    }
  50. };
  51.  
  52. void displayInvocation(void){
  53.    cout << "GREP : Demo for str (Version 2.11)  Copyright (c) 1992 Roy S. Woll"
  54.         << endl;
  55.    cout << "Syntax:   GREP [-iln] SearchString File[s]" << endl;
  56.    cout << "     -i Case insensitive search" << endl;
  57.    cout << "     -l list only file names" << endl;
  58.    cout << "     -n list line numbers" << endl;
  59.    exit(1);
  60. };
  61.  
  62. int main(int argc, char * argv[]){
  63.    if (argc<2) displayInvocation();
  64.    str filespec, options;
  65.  
  66.    if (!strncmp(argv[1],"-", 1)) {
  67.       if (argc <3) displayInvocation();
  68.       options = argv[1];
  69.       pattern = argv[2];
  70.       filespec = argv[3];
  71.       if (options.search("i")) line.setCaseSensitive(0);
  72.       if (options.search("n")) enableLineNumbers = 1;
  73.       if (options.search("l")) fileNamesOnly=1;
  74.    }
  75.    else {
  76.       pattern = argv[1];
  77.       filespec = argv[2];
  78.    };
  79.  
  80.    ffblk fileblk;
  81.    if (findfirst(filespec, &fileblk, 0))
  82.       cout << endl << "*** No Files Found! " << endl;
  83.    else {
  84.       int pos=0, lastpos=-1;
  85.       while (filespec.search("\\", &pos)) lastpos = pos++;
  86.       str directory = filespec(0, lastpos+1);
  87.       grepfile(directory + fileblk.ff_name);
  88.       while (!findnext(&fileblk)) grepfile(directory + fileblk.ff_name);
  89.    };
  90.    return 0;
  91. };
  92.  
  93.